//
//  DBMainController.swift
//  大波微博swift
//
//  Created by 大波 on 16/2/26.
//  Copyright © 2016年 zhaojin. All rights reserved.
//

import UIKit
import SVProgressHUD

class DBMainController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //        tabBar.tintColor = UIColor.orangeColor()
        
        let mainTabBar = DBMainTabBar()
        
        setValue(mainTabBar, forKey: "tabBar")
        
        mainTabBar.composeDelegate = self
        
        addChildViewControllers()
    }
    
    func addChildViewControllers() {
        
        let homeVC = DBHomeController()
        
        addChildViewController(homeVC, title: "首页", imageName: "tabbar_home")
        
        let discoverVC = DBDiscoverController()
        
        addChildViewController(discoverVC, title: "发现", imageName: "tabbar_discover")
        
        //添加占位控制器
        addChildViewController(UIViewController())
        
        let messageVC = DBMessageController()
        
        addChildViewController(messageVC, title: "消息", imageName: "tabbar_message_center")
        
        let profileVC = DBProfileController()
        
        addChildViewController(profileVC, title: "我", imageName: "tabbar_profile")
    }
    
    func addChildViewController(childVC: UIViewController, title: String, imageName: String) {
        
        childVC.title = title
        
        childVC.tabBarItem.image = UIImage(named: imageName)
        
        let highlightedImage = imageName + "_highlighted"
        
        childVC.tabBarItem.selectedImage = UIImage(named: highlightedImage)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        childVC.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Selected)
        
        addChildViewController(UINavigationController(rootViewController: childVC))
    }
}

//MARK: - DBMainTabBarDelegate 代理方法
extension DBMainController: DBMainTabBarDelegate {
    
    func mainTabBarButtomDidClick() {
        
//        print(__FUNCTION__)
        
        if DBUserAccountViewModel.shareUserAccountViewModel.isUserLogin {
            
            let composeVC = DBComposeController()
            
            presentViewController(UINavigationController(rootViewController: composeVC), animated: true, completion: nil)
            
        } else {
            
            SVProgressHUD.showErrorWithStatus("请先登录")
        }
    }
}

